package com.ohuang.demo

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.graphics.Canvas
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.MeasurePolicy
import androidx.compose.ui.layout.Placeable
import androidx.compose.ui.layout.layout
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.ohuang.demo.kotlin.MyList
import com.ohuang.demo.page.MainPage
import com.ohuang.demo.ui.theme.DemoTheme
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flow

class MainActivity : ComponentActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        setContent {
            DemoTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    Column(modifier = Modifier.padding(innerPadding)) {
                        MainPage()
                    }
                }
            }
        }
    }
}

private const val TAG = "MainActivity"
@Composable
fun testSideEffect(int: Int): Int{
    SideEffect {
        Log.d(TAG, "testSideEffect SideEffect")
    }
    Log.d(TAG, "testSideEffect ")
    return int+1
}

@Composable
fun TestLayout(content: @Composable () -> Unit) {
    Layout(content = content, measurePolicy = MeasurePolicy { measurables, constraints ->
        val placeables = arrayListOf<Placeable>()
        val size = measurables.size
        Log.d(TAG, "TestLayout: size=$size")
        measurables.forEach {
            val placeable = it.measure(constraints)
            placeables.add(placeable)
        }
        layout(constraints.maxWidth, constraints.maxHeight) {
            placeables.forEach {
                it.place(0, 0)
            }
        }
    })
}
@Composable
fun TestList(list : MyList<String>, onclick:(Int)->Unit){
    Column() {
        Row {
            Button(onClick = {onclick(0)}) {
                Text(text = "添加数据")
            }
            Button(onClick = {onclick(1)}) {
                Text(text = "删除数据")
            }
            Button(onClick = {onclick(2)}) {
                Text(text = "修改数据")
            }
        }
        for (i in 0 until  list.size()){
            Text(text = list.get(i))
        }
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Column(modifier = Modifier.padding(20.dp)) {
        TestLayout {
            var intState=remember { mutableStateOf(0) }
            Text("123", modifier = Modifier.layout { measurables, constraints ->
                val measure = measurables.measure(constraints)
                layout(measure.width, measure.height) {
                    measure.placeRelative(200, 0)
                }
            })
            Text("321", )
            Column() {
                Text("456")
                Text("789",Modifier.clickable{
                    intState.value++
                })
            }
            LaunchedEffect(Unit) {
                Log.d(TAG, "TestLayout: ")
            }
            var a=testSideEffect(intState.value)
            Log.d(TAG, "TestLayout: $a")
        }
        Text(
            text = "Hello $name!",
            modifier = modifier
        )

    }
    Log.d(TAG, "Greeting: ")
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    DemoTheme {
        Greeting("Android")
    }
}